package com.mhz.test.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mhz.test.pojo.StatisticsDaily;
import com.mhz.test.service.StatisticsDailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 前端控制器
 * </p>
 *
 * @author mhz
 * @since 2021-04-03
 */
@RestController
@RequestMapping("")
public class StatisticsDailyController {

    @Autowired
    StatisticsDailyService dailyService;

    @GetMapping("line")
    public Map<String, Object> showLineChart(Model model) {
        QueryWrapper<StatisticsDaily> dayQueryWrapper = new QueryWrapper<>();//构造查询条件
        dayQueryWrapper.select("login_num", "date_calculated");//向条件中添加条件，这里的意思是只查询表中“login_num”，“date_calculated”两个字段的数据
        List<StatisticsDaily> dayList = dailyService.list(dayQueryWrapper);//执行语句，得到数据

        //对数据进行处理，得到前端需要的数据结构
        List<String> calculatedList = new ArrayList<>();
        List<Integer> numDateList = new ArrayList<>();
        for (StatisticsDaily daily : dayList) {
            calculatedList.add(daily.getDateCalculated());
            numDateList.add(daily.getLoginNum());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("calculatedList", calculatedList);
        map.put("numDateList", numDateList);
        return map;
    }
}

